<?php

class system_queue_adapter_mysql implements system_interface_queue_adapter {

    private $__model = NULL;

    function __construct() {
        $this->__model = app::get('system')->model('queue_mysql');
    }

    public function publish($queue_name, $queue_data) {
        $time = time();
        $data = array(
            'queue_name' => $queue_data['queue_name'],
            'worker' => $queue_data['worker'],
            'params' => serialize((array) $queue_data['params']),
            'create_time' => $time
        );
        
        return $this->__model->insert($data);
    }

    public function get($queue_name) {
        if (($row = $this->__model->get($queue_name))) {
            $queue_data = array(
                'id' => $row['id'],
                'params' => unserialize($row['params']),
                'worker' => $row['worker'],
                'create_time' => $row['create_time']
            );
            return new system_queue_message_mysql($queue_data);
        }
        return false;
    }

    public function ack($queue_message) {
        $queue_id = $queue_message->get_id();
        return $this->__model->delete(array(
            'id' => $queue_id
        ));
    }

    public function purge($queue_name) {
        return $this->__model->purge($queue_name);
    }

    public function is_end($queue_name) {
        return $this->__model->is_end($queue_name);
    }

    public function consume($queue_name) {}
}

